﻿using System;
using System.ServiceModel;
using System.Text;

using WSU.Cheka.Session.Contracts;

namespace WSU.Cheka.Session.Client
{
    sealed class CalClientProxy : ClientBase<ICalculator>, ICalculator
    {
        public CalClientProxy(string endPntCfgName)
            : base(endPntCfgName)
        { }

        public void Calculate(float x, float y)
        {
            base.Channel.Calculate(x, y);
        }

        public float[] GetResults()
        {
            return base.Channel.GetResults();
        }
    }

    static class Program
    {
        private static StringBuilder m_strBuilder = new StringBuilder();

        private static string Trans2String(this float[] array)
        {
            m_strBuilder.Clear();
            m_strBuilder.Append("[");

            for (int index = 0; index < array.Length; ++index)
            {
                m_strBuilder.Append(array[index]);
                if (index != array.Length - 1)
                    m_strBuilder.Append(",");
            }
            m_strBuilder.Append("]");

            return m_strBuilder.ToString();
        }

        static void Main(string[] args)
        {
            Console.WriteLine("************** TCP client");
            using (CalClientProxy proxy1 = new CalClientProxy("TcpClient"))
            {
                // the first call start the session at the server side
                proxy1.Calculate(1, 2);
                Console.WriteLine("result = {0}", proxy1.GetResults().Trans2String());

                proxy1.Calculate(23, 55);
                Console.WriteLine("result = {0}", proxy1.GetResults().Trans2String());
            }

            Console.WriteLine("Press any key to start another proxy, ......");
            Console.ReadLine();

            Console.WriteLine("************** Named Pipe client");
            using (CalClientProxy proxy2 = new CalClientProxy("PipeClient"))
            {
                proxy2.Calculate(3, 4);
                Console.WriteLine("result = {0}", proxy2.GetResults().Trans2String());
            }
        }
    }
}
